package com.ltkj.baseplatform.controller;

import com.ltkj.baseplatform.domain.*;
import com.ltkj.baseplatform.domain.bo.ArtContentBo;
import com.ltkj.baseplatform.domain.bo.ArtContentQueryBo;
import com.ltkj.baseplatform.domain.vo.ArtContentVo;
import com.ltkj.baseplatform.service.IArtContentService;
import com.ltkj.common.core.web.controller.BaseController;
import com.ltkj.common.core.web.domain.AjaxResult;
import com.ltkj.common.core.web.page.TableDataInfo;
import com.ltkj.common.log.annotation.Log;
import com.ltkj.common.log.enums.BusinessType;
import com.ltkj.common.security.annotation.RequiresPermissions;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 文章内容Controller
 * 
 * @author liupf
 * @date 2023-08-28
 */
@RestController
@RequestMapping("/content")
@Api(tags = "文章管理-文章内容相关", description = "文章管理-文章内容相关")
public class ArtContentController extends BaseController
{
    @Autowired
    private IArtContentService artContentService;

    /**
     * 查询文章内容列表
     */
    @RequiresPermissions("baseplatform:content:list")
    @GetMapping("/list")
    @ApiOperation("查询文章内容列表")
    public TableDataInfo list(ArtContentQueryBo artContentQueryBo)
    {
        startPage();
        List<ArtContentVo> list = artContentService.selectArtContentList(artContentQueryBo);
        return getDataTable(list);
    }

    /**
     * 获取文章内容详细信息
     */
    @RequiresPermissions("baseplatform:content:query")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取文章内容详细信息")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(artContentService.selectArtContentById(id));
    }

    /**
     * 新增文章内容
     */
    @RequiresPermissions("baseplatform:content:add")
    @Log(title = "文章内容", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增文章内容")
    public AjaxResult add(@Validated() @RequestBody ArtContentBo artContentBo)
    {
        return toAjax(artContentService.insertArtContent(artContentBo));
    }

    /**
     * 修改文章内容
     */
    @RequiresPermissions("baseplatform:content:edit")
    @Log(title = "文章内容", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改文章内容")
    public AjaxResult edit(@Validated() @RequestBody ArtContentBo artContentBo)
    {
        return toAjax(artContentService.updateArtContent(artContentBo));
    }

    /**
     * 删除文章内容
     */
    @RequiresPermissions("baseplatform:content:remove")
    @Log(title = "文章内容", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    @ApiOperation("删除文章内容")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(artContentService.deleteArtContentByIds(ids));
    }

    /**
     * 将文章数据放入回收站
     */
    @RequiresPermissions("baseplatform:content:edit")
    @Log(title = "文章内容", businessType = BusinessType.UPDATE)
    @PostMapping("/recycle")
    @Operation(summary = "将文章数据放入回收站", description = "将文章数据放入回收站")
    public AjaxResult recycle(@Validated @RequestBody SimpleValue.Ids ids)
    {
        return toAjax(artContentService.recycle(ids.getIds(),artContentService.selectArtContentListByIds(ids.getIds())));
    }


    /**
     * 点赞接口
     */
    @GetMapping("/likes")
    @ApiOperation("点赞")
    public AjaxResult addLikes(@RequestParam("id") Long id)
    {
        return toAjax(artContentService.addLikes(id));
    }

    /**
     * 取消点赞
     */
    @GetMapping("/cancel/likes")
    @ApiOperation("取消点赞")
    public AjaxResult cancelLikes(@RequestParam("id") Long id)
    {
        ArtContent artContent = artContentService.selectContentById(id);
        if(artContent == null){
            AjaxResult.error("文章数据不存在！");
        }
        //如果点赞数为0还要取消点赞，就返回操作成功（理论上只会在联调阶段出现该问题）
        if(artContent.getLikes() == null || artContent.getLikes().equals(0L)){
            AjaxResult.success();
        }
        return toAjax(artContentService.cancelLikes(id));
    }

    /**
     * 收藏接口
     */
    @PostMapping("/collect")
    @ApiOperation("收藏")
    public AjaxResult addCollect(@RequestBody SerCollectData serCollectData)
    {
        return toAjax(artContentService.addCollect(serCollectData));
    }

    /**
     * 取消收藏
     */
    @GetMapping("/cancel/colect")
    @ApiOperation("取消收藏")
    public AjaxResult cancelColect(@RequestParam("id") Long id)
    {
        ArtContent artContent = artContentService.selectContentById(id);
        if(artContent == null){
            AjaxResult.error("文章数据不存在！");
        }
        //如果收藏数为0还要取消收藏，就返回操作成功（理论上只会在联调阶段出现该问题）
        if(artContent.getCollectNum() == null || artContent.getCollectNum().equals(0L)){
            AjaxResult.success();
        }
        return toAjax(artContentService.cancelColect(id));
    }

    /**
     * 发布评论接口
     */
    @PostMapping("/publish/comment")
    @ApiOperation("发布评论")
    public AjaxResult publishComment(@RequestBody SerCommentData serCommentData)
    {
        return toAjax(artContentService.publishComment(serCommentData));
    }

    /**
     * 删除评论
     */
    @GetMapping("/delete/comment")
    @ApiOperation("删除评论")
    public AjaxResult deleteComment(@RequestParam("id") Long id)
    {
        return toAjax(artContentService.deleteComment(id));
    }

    /**
     * 查询文章内容列表
     */
    @GetMapping("/collect/list")
    @ApiOperation("查询我的收藏列表")
    public TableDataInfo collectList()
    {
        startPage();
        return getDataTable(artContentService.selectSerCollectDataList());
    }

    /**
     * 浏览量+1
     */
 /*
    @GetMapping("/add/read/num")
    @ApiOperation("浏览量+1")
    public AjaxResult addReadNum(@RequestParam("id") Long id)
    {
        return toAjax(artContentService.updateReadNum(id));
    }
*/
    /**
     * 导出文章内容列表
     */
    /*@RequiresPermissions("system:content:export")
    @Log(title = "文章内容", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @Operation(summary = "导出文章内容列表", description = "导出文章内容列表")
    public void export(HttpServletResponse response, ArtContent artContent)
    {
        List<ArtContent> list = artContentService.selectArtContentList(artContent);
        ExcelUtil<ArtContent> util = new ExcelUtil<ArtContent>(ArtContent.class);
        util.exportExcel(response, list, "文章内容数据");
    }*/
}
